import { defineConfig, loadEnv } from "vite"
import path from "path"
import ElementPlus from "unplugin-element-plus/vite"
import AutoImport from "unplugin-auto-import/vite"
import Components from "unplugin-vue-components/vite"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import vue from "@vitejs/plugin-vue"
import UnoCSS from "unocss/vite"
import proxy from "./src/http/proxy"
// https://vitejs.dev/config/

const pathSrc = path.resolve(__dirname, "src")
export default ({ mode }: { mode: string }) => {
  const env = loadEnv(mode, process.cwd())
  console.log("当前环境", env)
  return defineConfig({
    plugins: [
      UnoCSS({ inspector: false }),
      vue(),
      ElementPlus({}),
      AutoImport({
        resolvers: [
          ElementPlusResolver({
            // 自动引入修改主题色添加这一行，使用预处理样式，不添加将会导致使用ElMessage，ElNotification等组件时默认的主题色会覆盖自定义的主题色
            importStyle: "sass"
          })
        ],
        dts: path.resolve(pathSrc, "auto-imports.d.ts")
      }),
      Components({
        resolvers: [
          ElementPlusResolver({
            // 自动引入修改主题色添加这一行，使用预处理样式
            importStyle: "sass"
          })
        ],
        dts: path.resolve(pathSrc, "components.d.ts")
      })
    ],
    resolve: {
      alias: {
        "@": pathSrc
      }
    },
    base: "./",
    css: {
      preprocessorOptions: {
        scss: {
          api: "modern-compiler",
          // 自定义的主题色
          additionalData: `@use "@/assets/element/index.scss" as *;`
        },
        less: {
          additionalData: `@import "${path.resolve(__dirname, "src/assets/less/pxTovwvh.less")}"; `
        }
      }
    },
    build: {
      target: "modules",
      outDir: "admissions-website", //项目打包文件夹名称, 如nginx 使用root 配置,需与 location 的值一致
      chunkSizeWarningLimit: 1024,
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      },
      rollupOptions: {
        output: {
          manualChunks: {
            echarts: ["echarts"],
            elementPlus: ["element-plus"]
          }
        }
      }
    },
    server: {
      proxy: {
        ...proxy
      }
    }
  })
}
